package cn.jbolt.util;

import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;

@Component
public class FilePathGeneratorUtil {


    public static String generateFilePath(String template, String originalFileName) {
        String oriFileName = originalFileName;
        String oriSuffix = "";
        int dotIndex = originalFileName.lastIndexOf('.');
        if (dotIndex != -1) {
            // 修正：这里只截取后缀名部分，不包含点
            oriSuffix = originalFileName.substring(dotIndex + 1);
            oriFileName = originalFileName.substring(0, dotIndex);
        }

        template = template.replace("{ori}", originalFileName)
                .replace("{oriSuffix}", oriSuffix)
                .replace("{oriName}", oriFileName);

        // 处理随机字符
        int randomStart = template.indexOf("{random(");
        while (randomStart != -1) {
            int randomEnd = template.indexOf(")}", randomStart);
            if (randomEnd != -1) {
                String lengthStr = template.substring(randomStart + 8, randomEnd);
                int length = Integer.parseInt(lengthStr);
                String randomStr = generateRandomString(length);
                template = template.replace(template.substring(randomStart, randomEnd + 2), randomStr);
            }
            randomStart = template.indexOf("{random(");
        }

        // 处理日期
        int dateStart = template.indexOf("{date(");
        while (dateStart != -1) {
            int dateEnd = template.indexOf(")}", dateStart);
            if (dateEnd != -1) {
                String formatStr = template.substring(dateStart + 6, dateEnd);
                DateTimeFormatter formatter;
                if (formatStr.isEmpty()) {
                    formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
                } else {
                    formatter = DateTimeFormatter.ofPattern(formatStr);
                }
                String dateStr = LocalDateTime.now().format(formatter);
                template = template.replace(template.substring(dateStart, dateEnd + 2), dateStr);
            }
            dateStart = template.indexOf("{date(");
        }
        template = template.replace("{date}", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));

        return template;
    }

    private static String generateRandomString(int length) {
        String characters = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(characters.length());
            sb.append(characters.charAt(index));
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String template = "/doc/{date}/{ori}";
        String originalFileName = "example.doc";
        String filePath = generateFilePath(template, originalFileName);
        System.out.println(filePath);
    }
}